package com.pig.jjfetion.core.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

import org.apache.log4j.Logger;

public class FetionConfig {

	private static Properties prop;
	private static boolean initialized;
	private static Logger logger = Logger.getLogger(FetionConfig.class);

	/*
	 * 初使化，同步方法
	 */
	public synchronized static void init(){
	
		if(!initialized){
			loadDefaultConfig();
			loadUserConfig();
		}
	}
	
	/*
	 * 返回配置属性
	 */
	public static Properties getProperties(){
		return prop;
	}
	
	/*
	 * 返回指定key的字符串配置
	 * @param name
	 */
	public static String getString(String key){
		if(prop.getProperty(key)!=null){
			return prop.getProperty(key);
		}else{
			throw new IllegalArgumentException("Can not find this key in configs. key="+key);
		}
	}
	/*
	 * 设置指定key的字符串配置
	 * @param name
	 * @param value
	 */
	public static void setString(String key, String value){
		prop.setProperty(key, value);
	}
	/*
	 *返回指定key的整数配置
	 *@param key
	 *@return 
	 */
	public static int getInteger(String key){
		return Integer.parseInt(getString(key));
	}
	/*
	 * 设置指定key的整数配置
	 * @param key
	 * @param value
	 */
	public static void setInteger(String key, int value){
		prop.setProperty(key, Integer.toString(value));
	}
	/*
	 * 返回指定key的布尔配置
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(String key){
		return Boolean.parseBoolean(getString(key));
	}
	/*
	 * 设置指定key的布尔配置
	 * @param name
	 * @param value
	 */
	public static void setBoolean(String key, boolean value){
		prop.setProperty(key, Boolean.toString(value));
	}
	
	
	/*
	 * 加载默认的配置文件
	 * 在类的根目录中查找：jjfetion.default.properties
	 * 这个文件定义了所有飞信的配置
	 */
	private static void loadDefaultConfig(){
		InputStream in = FetionConfig.class.getResourceAsStream("/resources/jjfetion.default.properties");
		if(in != null){
			prop = new Properties();
			try {
				prop.load(in);
			} catch (IOException e) {
				throw new IllegalStateException("Error occured when loading jjfetion.default.properties in classpath.");
			}
		}else{
			throw new IllegalStateException("Cannot find jjfetion.default.properties in classpath.");
		}
	}
	
	/*
	 * 加载用户定义配置，默认存在程序启动目录
	 */
	private static void loadUserConfig(){
		File file = new File("jjfetion.properties");
		if(file.exists()&&file.canRead()){
			try {
				logger.debug("found user specified config in jjfetion.properties..");
				loadConfig(file);
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}
		}
	}
	/*
	 * 合并默认配置和用户配置
	 * @param file
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void loadConfig(File file) throws FileNotFoundException, IOException{
		Properties userDefinedProperties = new Properties();
		userDefinedProperties.load(new FileInputStream(file));
		Iterator<?> it = userDefinedProperties.keySet().iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			prop.setProperty(key, userDefinedProperties.getProperty(key));
		}
	}
}
